package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.BailOrderBean;
import com.bocai.kelalacar.bean.ConfigBean;
import com.bocai.kelalacar.bean.MessageEvent;
import com.bocai.kelalacar.pay.alipay.AlipayApi;
import com.bocai.kelalacar.pay.simcpux.WxPayApi;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.ServiceMarginContract;
import com.bocai.kelalacar.ui.car.set.module.ServiceMarginModule;
import com.bocai.kelalacar.ui.car.set.presenter.ServiceMarginPresenter;
import com.bocai.kelalacar.util.DoubleUtil;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.widget.PayPopupWindow;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanfei on 2017/6/23.
 */

public class ServiceMarginActivity extends BaseActivity implements View.OnClickListener, ServiceMarginContract.view {

    PayPopupWindow popupWindow;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll_pay)
    LinearLayout llPay;
    @BindView(R.id.btn_withdraw)
    Button btnWithdraw;
    @BindView(R.id.ll_withdraw)
    LinearLayout llWithdraw;
    @BindView(R.id.btn_pay)
    Button btnPay;
    @Inject
    ServiceMarginPresenter presenter;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_money1)
    TextView tvMoney1;
    @BindView(R.id.main)
    LinearLayout main;
    private double money;

    @Override
    public int getLayoutId() {
        return R.layout.act_service_margin;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new ServiceMarginModule(this)).inject(this);
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        initToolbar("服务保证金");
        initBack(imgBack, llBack);
        popupWindow = new PayPopupWindow(this, "1000", this);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        btnWithdraw.setOnClickListener(this);
        btnPay.setOnClickListener(this);
        presenter.getConfig();
        if ((boolean) SP.get(this, "PayBail", true)) {
            llWithdraw.setVisibility(View.VISIBLE);
            llPay.setVisibility(View.GONE);
            btnPay.setVisibility(View.GONE);
        } else {
            llWithdraw.setVisibility(View.GONE);
            llPay.setVisibility(View.VISIBLE);
            btnPay.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pay:
                backgroundAlpha(0.5f);
                popupWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.btn_confirm_pay:
                presenter.bailOrder();
                break;
            case R.id.btn_withdraw:
                Intent intent = new Intent(ServiceMarginActivity.this, WithdrawActivity.class);
                intent.putExtra("index", 2);
                intent.putExtra("money", money);
                startActivityForResult(intent, 135);
                break;

        }
    }

    @Override
    public void bailOrderSuccess(BailOrderBean bean) {
        if (popupWindow.getPayType() == 0) {
            presenter.orderPay("alipay", bean.getOrderId());
        } else {
            presenter.orderPay("weixin", bean.getOrderId());
        }
    }

    @Override
    public void orderPaySuccess(String result) {
        popupWindow.dismiss();
        if (popupWindow.getPayType() == 0) {
            AlipayApi alipayApi = new AlipayApi(this);
            alipayApi.payV2(result);
        } else {
            WxPayApi wxPayApi = new WxPayApi(this);
            wxPayApi.pay(result);
        }

    }

    @Override
    public void getConfig(ConfigBean bean) {
        tvMoney.setText("需要交纳服务保证金"+DoubleUtil.m2(bean.getBail())+"元");
        tvMoney1.setText(DoubleUtil.m2(bean.getBail())+"");
        popupWindow.setValue(bean.getBail());
        money = bean.getBail();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MessageEvent event) {
        if ("11".equals(event.msg)) {
            SP.put(this, "PayBail", true);
            llWithdraw.setVisibility(View.VISIBLE);
            llPay.setVisibility(View.GONE);
            btnPay.setVisibility(View.GONE);
        }
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")) {
            startActivity(new Intent(this, MainActivity.class).putExtra("index", 1));
            finish();
        }
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 135 && resultCode == 222) {
            if ((boolean) SP.get(this, "PayBail", true)) {
                llWithdraw.setVisibility(View.VISIBLE);
                llPay.setVisibility(View.GONE);
                btnPay.setVisibility(View.GONE);
            } else {
                llWithdraw.setVisibility(View.GONE);
                llPay.setVisibility(View.VISIBLE);
                btnPay.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
